package byscaler

import (
	"bylib/bylog"
	byutil "bylib/byutils"
	"time"
)

//传感器协议接口.
type SensorProtocol interface {
	marshal()[]byte
}
type Addr int32

type State struct{
	Positive  bool //正负号 0 正 1 负数
	SensorErr bool 	//传感器故障、脱落 bit5  0 正常 1 故障
	Error  bool		//设备故障. bit3
	Overflow bool 	//重量溢出. bit2
	Still bool 		//是否稳定.	bit1
	Zero bool 		//是否零位. bit0
}

//单个传感器
type Sensor struct{
	Addr  int32 `json:"addr"`//传感器地址
	Value int32 `json:"value"`//传感器的原始值
	PwrValue int32 `json:"pwr_value"` //减去零点后的值.
	CalcValue int32 `json:"calc_value"`//计算值 = 原始值-开机零点-跟踪零点
	State  State //状态.
	StateValue uint8 `json:"state_value"`
	Timeout int32 `json:"timeout"`//超时计数器.
	TimeStamp int64 `json:"time_stamp"`//采集的时间戳.
	Online bool `json:"online"`
	StillCount uint8 //稳定计数器
	//开机零点跟踪
	PwrZero int32 //开机零点
	PwrStable int32 //
	Zero int32 //当前跟踪的零点值.
	OldValue int32 //上次的值
	IsConnected bool `json:"is_connected"`
	ConnTs int64 `json:"conn_ts"`
	
	ZeroFlag bool //启动了零点跟踪
	OldCalcValue int32 //上次的零点跟踪值.
	ZeroTs int64 `json:"zero_ts"`

	PowerOn bool `json:"power_on"` //是否上电第一次.
	PowerOnTs int64 `json:"power_on_ts"`
}


func NewSensor(addr int32)*Sensor  {
	return &Sensor{
		Value:0,
		Addr:addr,
		State:State{

		},
		Online:false,
		Timeout:0,
		OldValue:0,
		ConnTs:0,
		IsConnected:false,
		Zero:0,
		PwrValue:0,
		PwrZero:0,
		ZeroFlag:false,
		ZeroTs: 0,
		PowerOn:true,
		PowerOnTs:time.Now().Unix(),
	}
}
//零点跟踪函数，返回是否可以存储.
func (s *Sensor )ZeroTrack(maxValue,delayS int32)bool  {

	if s.Addr == 1 {
		bylog.Debug("value=%d pwr=%d calc=%d flag=%v",s.Value,s.PwrValue,s.CalcValue,s.ZeroFlag)
	}
	if !s.Online {
		return false
	}
	if byutil.Abs2(s.CalcValue) <= maxValue   && s.State.Still{
		if !s.ZeroFlag{
			s.ZeroFlag = true
			s.ZeroTs = time.Now().Unix()
		}
	}else{
		s.ZeroFlag = false
	}
	if s.ZeroFlag{
		//如果设置过标志，到了时间就清零.
		delay:=time.Now().Unix() - s.ZeroTs
		bylog.Debug("diff=%d",delay)
		if int32(delay) >= delayS {
			s.ZeroFlag  = false
			s.ZeroTs = time.Now().Unix()
			if byutil.Abs2(s.CalcValue) <= maxValue && s.State.Still{
				bylog.Debug("zero=%d",s.Zero)
				s.Zero += s.CalcValue
			}

			return true
		}
	}
	return false
}
func (s *Sensor )ClearZero(){
	s.PwrZero = 0
	s.Zero = 0
}
//零点跟踪函数，返回是否跟踪成功.
func (s *Sensor )PowerOnTrack(maxValue,delayS int32)bool  {
	if !s.Online {
		return false
	}
	now:=time.Now().Unix()
	if s.Value != s.OldValue && s.OldValue >= 65535 && !s.IsConnected{
		s.IsConnected = true
		s.PwrStable = 0
		//s.Zero = 0
		s.ConnTs = time.Now().Unix()
	}
	s.OldValue = s.Value
	if s.Value >= 65535{
		s.IsConnected = false
		s.PwrStable = 0
		s.Zero = 0

		return false
	}
	//开机置零
	if s.PowerOn{
		if (now - s.PowerOnTs) > int64(delayS){
			s.PowerOn = false
			if  byutil.Abs2(s.Value) < maxValue && s.State.Still {
				bylog.Debug("power on ok----------- value=%d",s.Value)
				s.PwrZero = s.Value
				return true
			}
		}
	}
	if s.IsConnected{
		delay:= now - s.ConnTs
		if int32(delay) >= delayS {
			s.IsConnected = false

			s.ConnTs = now
			if  byutil.Abs2(s.Value) < maxValue && s.State.Still{
				bylog.Debug("power on ok----------- value=%d",s.Value)
				s.PwrZero = s.Value
				return true
			}
		}else {
			if s.State.Still{
				s.PwrStable++
			}else{
				s.PwrStable = 0
			}
		}
	}
	return false
}